package xuhaijia.lanou3g.autohome.findcar.depreciate;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DecimalFormat;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.NetTool;

/**
 * Created by 7 on 16/8/24.
 */
public class DepreciateAdapter extends BaseAdapter {
    private Context context;
    private DepreciateBean depreciateBean;
    private NetTool mNetTool = new NetTool();

    public DepreciateAdapter(Context context) {
        this.context = context;
    }

    public void setDepreciateBean(DepreciateBean depreciateBean) {
        this.depreciateBean = depreciateBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (depreciateBean.getResult().getCarlist() == null) {
            return 0;
        }
        return depreciateBean.getResult().getCarlist().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_depreciate, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DepreciateBean.ResultBean.CarlistBean bean = depreciateBean.getResult().getCarlist().get(position);
        mNetTool.getImage(bean.getSpecpic() ,viewHolder.iv);
        viewHolder.titleTv.setText(bean.getSeriesname() + "  " + bean.getSpecname());
        viewHolder.priceNowTv.setText(bean.getDealerprice() + "万");
        viewHolder.priceOldTv.setText(bean.getFctprice() + "万");
        viewHolder.priceOldTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        float oldPrice = Float.parseFloat(bean.getFctprice());
        float nowPrice = Float.parseFloat(bean.getDealerprice());
        float difference = oldPrice - nowPrice ;
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        String cha = decimalFormat.format(difference);
        viewHolder.differenceTv.setText("降" + cha + "万");
        viewHolder.cityTv.setText(bean.getDealer().getCity());
        viewHolder.shortNameTv.setText(bean.getDealer().getShortname());
        viewHolder.distanceTv.setText("距离" + bean.getDealer().getDistance());
        viewHolder.whereBuyTv.setText(bean.getOrderrange());
        if (bean.getDealer().getIs24hour() == 1) {
            viewHolder.isAllDayTv.setVisibility(View.VISIBLE);
        } else {
            viewHolder.isAllDayTv.setVisibility(View.GONE);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView iv;
        TextView titleTv;
        TextView priceNowTv;
        TextView priceOldTv;
        TextView differenceTv;
        TextView cityTv;
        TextView shortNameTv;
        TextView distanceTv;
        TextView whereBuyTv;
        TextView isAllDayTv;
        LinearLayout counter;
        LinearLayout phoneCall;
        LinearLayout askMoney;
        public ViewHolder(View view) {
            iv = (ImageView) view.findViewById(R.id.item_depreciate_iv);
            titleTv = (TextView) view.findViewById(R.id.item_depreciate_title);
            priceNowTv = (TextView) view.findViewById(R.id.item_depreciate_nowprice);
            priceOldTv = (TextView) view.findViewById(R.id.item_depreciate_oldprice);
            differenceTv = (TextView) view.findViewById(R.id.item_depreciate_difference);
            cityTv = (TextView) view.findViewById(R.id.item_depreciate_city);
            shortNameTv = (TextView) view.findViewById(R.id.item_depreciate_shortname);
            distanceTv = (TextView) view.findViewById(R.id.item_depreciate_distance);
            whereBuyTv = (TextView) view.findViewById(R.id.item_depreciate_wherebuy);
            isAllDayTv = (TextView) view.findViewById(R.id.item_depreciate_isallday);
            titleTv = (TextView) view.findViewById(R.id.item_depreciate_title);
            counter = (LinearLayout) view.findViewById(R.id.item_depreciate_counter);
            phoneCall = (LinearLayout) view.findViewById(R.id.item_depreciate_phone);
            askMoney = (LinearLayout) view.findViewById(R.id.item_depreciate_askmoney);
        }
    }
}
